﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using AyuBookmark.Common.Models;

namespace AyuBookmark.Common.Interfaces
{
    /// <summary>
    /// 通用数据仓库接口，定义基本CRUD操作
    /// </summary>
    /// <typeparam name="TEntity">实体类型</typeparam>
    /// <typeparam name="TKey">主键类型</typeparam>
    public interface IRepository<TEntity, TKey> where TEntity : class
    {
        /// <summary>
        /// 根据主键获取实体
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns>查询到的实体</returns>
        Task<TEntity> GetByIdAsync(TKey id);

        /// <summary>
        /// 获取所有实体（谨慎使用大数据量场景）
        /// </summary>
        /// <returns>实体集合</returns>
        Task<IEnumerable<TEntity>> GetAllAsync();

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="predicate">查询条件表达式</param>
        /// <returns>匹配的实体集合</returns>
        Task<IEnumerable<TEntity>> FindAsync(Expression<Func<TEntity, bool>> predicate);

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="predicate">查询条件</param>
        /// <param name="pageIndex">页码（从1开始）</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="orderBy">排序表达式</param>
        /// <param name="isDescending">是否降序</param>
        /// <returns>分页结果</returns>
        Task<PaginatedResult<TEntity>> GetPagedAsync(
            Expression<Func<TEntity, bool>> predicate = null,
            int pageIndex = 1,
            int pageSize = 10,
            Expression<Func<TEntity, object>> orderBy = null,
            bool isDescending = true);

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity">要添加的实体</param>
        /// <returns>添加后的实体（包含生成的主键）</returns>
        Task<TEntity> AddAsync(TEntity entity);

        /// <summary>
        /// 批量添加实体
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <returns>添加的记录数</returns>
        Task<int> AddRangeAsync(IEnumerable<TEntity> entities);

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity">要更新的实体</param>
        /// <returns>更新是否成功</returns>
        Task<bool> UpdateAsync(TEntity entity);

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity">要删除的实体</param>
        /// <returns>删除是否成功</returns>
        Task<bool> DeleteAsync(TEntity entity);

        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns>删除是否成功</returns>
        Task<bool> DeleteByIdAsync(TKey id);

        /// <summary>
        /// 条件删除
        /// </summary>
        /// <param name="predicate">删除条件</param>
        /// <returns>删除的记录数</returns>
        Task<int> DeleteWhereAsync(Expression<Func<TEntity, bool>> predicate);

        /// <summary>
        /// 统计总记录数
        /// </summary>
        /// <param name="predicate">统计条件</param>
        /// <returns>记录数</returns>
        Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate = null);

        /// <summary>
        /// 判断是否存在满足条件的记录
        /// </summary>
        /// <param name="predicate">查询条件</param>
        /// <returns>是否存在</returns>
        Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> predicate);

        /// <summary>
        /// 执行SQL查询
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>实体集合</returns>
        Task<IEnumerable<TEntity>> QueryAsync(string sql, object parameters = null);

        /// <summary>
        /// 执行SQL命令
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>影响的行数</returns>
        Task<int> ExecuteAsync(string sql, object parameters = null);
    }

    ///// <summary>
    ///// 分页查询结果
    ///// </summary>
    ///// <typeparam name="T">数据类型</typeparam>
    //public class PaginatedResult<T>
    //{
    //    /// <summary>
    //    /// 当前页码
    //    /// </summary>
    //    public int PageIndex { get; set; }

    //    /// <summary>
    //    /// 每页记录数
    //    /// </summary>
    //    public int PageSize { get; set; }

    //    /// <summary>
    //    /// 总记录数
    //    /// </summary>
    //    public int TotalCount { get; set; }

    //    /// <summary>
    //    /// 总页数
    //    /// </summary>
    //    public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize);

    //    /// <summary>
    //    /// 当前页数据
    //    /// </summary>
    //    public IEnumerable<T> Items { get; set; } = new List<T>();

    //    /// <summary>
    //    /// 是否有上一页
    //    /// </summary>
    //    public bool HasPreviousPage => PageIndex > 1;

    //    /// <summary>
    //    /// 是否有下一页
    //    /// </summary>
    //    public bool HasNextPage => PageIndex < TotalPages;
    //}

    /// <summary>
    /// 书签数据仓库特定操作
    /// </summary>
    public interface IBookmarkRepository : IRepository<Bookmark, int>
    {
        /// <summary>
        /// 根据URL获取书签
        /// </summary>
        /// <param name="url">网页URL</param>
        /// <returns>匹配的书签</returns>
        Task<Bookmark> GetByUrlAsync(string url);

        /// <summary>
        /// 根据分类获取书签
        /// </summary>
        /// <param name="categoryId">分类ID</param>
        /// <returns>书签集合</returns>
        Task<IEnumerable<Bookmark>> GetByCategoryAsync(int categoryId);

        /// <summary>
        /// 批量更新书签分类
        /// </summary>
        /// <param name="bookmarkIds">书签ID集合</param>
        /// <param name="categoryId">目标分类ID</param>
        /// <returns>更新的记录数</returns>
        Task<int> UpdateCategoryAsync(IEnumerable<int> bookmarkIds, int categoryId);


        /// <summary>
        /// 获取需要验证的书签列表
        /// </summary>
        Task<List<Bookmark>> GetBookmarksNeedingValidationAsync();

        /// <summary>
        /// 标记书签为无效状态
        /// </summary>
        Task MarkAsInvalidAsync(int bookmarkId);

    }

    /// <summary>
    /// 分类数据仓库特定操作
    /// </summary>
    public interface ICategoryRepository : IRepository<Category, int>
    {
        /// <summary>
        /// 获取分类层级结构
        /// </summary>
        /// <returns>树形结构分类</returns>
        Task<IEnumerable<Category>> GetTreeStructureAsync();

        /// <summary>
        /// 获取热门分类（按使用次数排序）
        /// </summary>
        /// <param name="count">要获取的数量</param>
        /// <returns>分类集合</returns>
        Task<IEnumerable<Category>> GetPopularCategoriesAsync(int count = 5);
    }

    /// <summary>
    /// 配置项仓库特定操作
    /// </summary>
    public interface IConfigRepository : IRepository<ConfigItem, string>
    {
        /// <summary>
        /// 获取配置值（强类型）
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="key">配置键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>配置值</returns>
        Task<T> GetConfigValueAsync<T>(string key, T defaultValue = default);

        /// <summary>
        /// 设置配置值（强类型）
        /// </summary>
        /// <typeparam name="T">值类型</typeparam>
        /// <param name="key">配置键</param>
        /// <param name="value">要设置的值</param>
        Task SetConfigValueAsync<T>(string key, T value);
    }

}